﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using handvn.Models;
using System.Web.Security;

namespace handvn.Controllers
{ 
    public class UserController : Controller
    {
        private HandvnContext db = new HandvnContext();

        //
        // GET: /Product/
        public ViewResult View(string user)
        {
            //var detailcategory = db.DetailCategory.Where(d => d.CategoryID == DetailCategoryID);
            //return View(detailcategory.ToList());
            //var products = db.Products.Where(p => p.UserID == user);
            //return View(products.ToList());
            var queryResults2 = (from p in db.Products.Include("User")
                                 orderby p.SoLuotXem descending
                                 where (p.TinhTrang == true)
                                 select p).Take(8);
            var products = db.Products.Where(p => p.UserID == user && p.TinhTrang==true);
            var us = db.Users.Where(u => u.UserID == user && u.TinhTrang == true);
            var model = new UserView
            {
                Product = products.ToList(),
                User = us.ToList(),
            };
            return View(model);
        }

        //
        // GET: /User/

        public ViewResult Index()
        {
            var users = db.Users.Include(u => u.CategoriesUser);
            return View(users.ToList());
            
        }

        //
        // GET: /User/Details/5

        public ViewResult Details(string id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            ViewBag.CategoriesUserID = new SelectList(db.CategoriesUser, "CategoriesUserID", "TenLoaiNguoiDung");
            return View();
        } 

        //
        // POST: /User/Create

        //[HttpPost]
        //public ActionResult Create(User user)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Users.Add(user);
        //        db.SaveChanges();
        //        return RedirectToAction("Index");  
        //    }

        //    ViewBag.CategoriesUserID = new SelectList(db.CategoriesUser, "CategoriesUserID", "TenLoaiNguoiDung", user.CategoriesUserID);
        //    return View(user);
        //}
        [HttpPost]
        public ActionResult Create(User model)
        {
            //if (ModelState.IsValid)
            //{
            //    db.Users.Add(user);
            //    db.SaveChanges();
            //    return RedirectToAction("Index");
            //}

            //ViewBag.CategoriesUserID = new SelectList(db.CategoriesUser, "CategoriesUserID", "TenLoaiNguoiDung", user.CategoriesUserID);
            //return View(user);
            UserDAO usDAO = new UserDAO();
            if (usDAO.ktTrung(model) == true)
            {
                User dk = new User();
                dk.Username = model.Username;
                dk.Password1 = usDAO.GetMD5(model.Password1);
                dk.Password2 = usDAO.GetMD5(model.Password2);
                dk.TenKhachhang = model.TenKhachhang;
                dk.CMND = model.CMND;
                dk.DienThoai = model.DienThoai;
                dk.Email = model.Email;
                dk.DiaChi = model.DiaChi;
                dk.NgayDangKy = model.NgayDangKy;
                dk.HinhAnh = model.HinhAnh;
                dk.CategoriesUserID = model.CategoriesUserID;
                dk.TinhTrang = model.TinhTrang;

                string ngaythang = DateTime.Now.ToLocalTime().ToShortDateString();
                string[] ngaythangs = ngaythang.Split('/');
                string gio = DateTime.Now.ToLocalTime().ToShortTimeString();
                string[] gios = gio.Split(':');
                dk.UserID = "KH";
                foreach (string part in ngaythangs)
                {
                    dk.UserID = dk.UserID + part;
                }
                foreach (string part in gios)
                {
                    dk.UserID = dk.UserID + part;
                }
                string[] ma = dk.UserID.Split(' ');
                dk.UserID = ma[0];

                using (var db = new HandvnContext())
                {
                    db.Users.Add(dk);
                    db.SaveChanges();
                }
                FormsAuthentication.SetAuthCookie(model.Email, false);
                
                return RedirectToAction("Index");
            }
            else
            {
                ViewData["key"] = "Username này đã tồn tại";
                ViewBag.CategoriesUserID = new SelectList(db.CategoriesUser, "CategoriesUserID", "TenLoaiNguoiDung", model.CategoriesUserID);
                return View(model);
            }
            
        }
        
        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(string id)
        {
            User user = db.Users.Find(id);
            ViewBag.CategoriesUserID = new SelectList(db.CategoriesUser, "CategoriesUserID", "TenLoaiNguoiDung", user.CategoriesUserID);
            return View(user);
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CategoriesUserID = new SelectList(db.CategoriesUser, "CategoriesUserID", "TenLoaiNguoiDung", user.CategoriesUserID);
            return View(user);
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(string id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        //
        // POST: /User/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            User user = db.Users.Find(id);
            db.Users.Remove(user);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}